package com.example.studentmanager.common.aop.aspect;


import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

/**
 * 日志切面，主要是记录系统运行日志
 */


@Aspect
@Component
public class TestAop {

    private Logger logger = LoggerFactory.getLogger(TestAop.class);

    @Pointcut("execution(public * com.example.studentmanager.*..*(..))")
    public void testPointcut(){}

    @Before(value = "testPointcut()")
    public void before(JoinPoint joinPoint){
        String name=joinPoint.getSignature().getName();
        System.out.println("前置通知，方法名："+name+"开始执行");
        logger.info("前置通知："+name);
    }

    @After("testPointcut()")
    public void after(JoinPoint joinPoint){
        String name=joinPoint.getSignature().getName();
        System.out.println("后置通知，方法名："+name+"执行结束");
        logger.info("后置通知："+name);
    }


    @AfterReturning(value = "testPointcut()",returning = "result")
    public void afterReturn(JoinPoint joinPoint,Object result){
        String name=joinPoint.getSignature().getName();
        System.out.println("返回值通知，方法名："+name+"返回值为："+result);
    }

    @AfterThrowing(value = "testPointcut()",throwing = "e")
    public void afterThrow(JoinPoint joinPoint,Exception e){
        String name=joinPoint.getSignature().getName();
        System.out.println("异常通知，方法名："+name+"异常为："+e);

    }

    @Around("testPointcut()")
    public Object around(ProceedingJoinPoint proceedingJoinPoint)throws Throwable{
        return proceedingJoinPoint.proceed();
    }





}
